package com.ruoyi.project.app.util;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

//import com.sun.net.httpserver.Filter;

public class StringUtil extends StringUtils {
	public static final String SPECIAL_STRING_REX = "[`~!@#$%^&*':;',//[//].<>/?~@#%……&*]" ;
//	public static final String SPECIAL_STRING_REX = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]"; ;

	public static String trim(String str) {

		if (isEmpty(str)) {
			return null;
		} else {
			return str.trim();
		}

	}

	public static String empty(Object obj) {

		if (obj == null) {
			return "";
		}

		if (obj instanceof String) {
			return (String) obj.toString().trim();
		}

		return obj.toString();
	}

	/**
	 * 取得页面跳转url参数
	 * 
	 * @param map
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String getUrlParam(Map map) {

		Iterator iterator = map.entrySet().iterator();

		StringBuilder sBuilder = new StringBuilder("?page={page}");

		while (iterator.hasNext()) {

			Map.Entry entry = (Map.Entry) iterator.next();

			Object key = entry.getKey();

			Object value = entry.getValue();

			sBuilder.append("&" + key + "=" + empty(value));

		}

		return sBuilder.toString().trim();
	}

	public static String getUTF8Url(String s) {
		try {
			s = URLEncoder.encode(s, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return s;
	}

	public static boolean isEmpty(String s) {
		if (s == null || "".equals(s))
			return true;
		s = s.trim();
		return "".equals(s);
	}
	
	public static String joinString(List<String> list, String symbol)
	{
		String result = "";
		if (list != null)
		{
			for (int i=0; i<list.size(); i++)
			{
				String tmp = list.get(i).toString();
				if (tmp != null && tmp.trim().length() >0)
				{
					result += tmp + symbol;
				}
			}
		}
		if (result.length() >1 )
		{
			result = result.substring(0, result.length()-1);
		}
		return result;
	}

	public static String clearNull(String s) {
		return (s == null ? "" : s);
	}

	public static String StringFilter(String str) {

		// 清除掉所有特殊字符
		String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？\t|\r|\n]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	}

	public static void main(String[] args) {

//		try {
//			String s = URLDecoder.decode(
//					"%E9%AA%8C%E8%AF%81%E7%A0%81%E4%B8%8D%E6%AD%A3%E7%A1%AE",
//					"UTF-8");
//			System.out.println(s);
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		// Map map = new HashMap<String, Object>();
		//		
		// String value="e";
		// int value2=0;
		// map.put("title", value);
		// map.put("memberId", value2);
		//		
		// System.out.println(getUrlParam(map));
		String[] array = {"a", "b", "c"};
		String s = "a,b,c,d";
		System.out.println(splitToList(",", s));
		Long tmp ;
	}
	public static List<Long> strSplitToList(String src, String split)
	{
		List<Long> list = null;
		if (src != null)
		{
			// documentids array 转list
			String[] array= StringUtils.split(src,",");
			list = new ArrayList<Long>();
			for (int i=0; i<array.length; i++)
			{
				list.add(Long.valueOf(array[i]));
			}
		}
		return list;
	}
public static List<String> splitToList(String split, String src) 
{  
    String sp = ",";  
    if (split != null && split.length() == 1) 
    {  
        sp = split;  
    }  
    List<String> r = new ArrayList<String>();  
    int lastIndex = -1;  
    int index = src.indexOf(sp);  
    if (-1 == index && src != null) 
    {  
        r.add(src);  
        return r;  
    }  
    while (index >= 0) 
    {  
        if (index > lastIndex) 
        {  
              r.add(src.substring(lastIndex + 1, index));  
        } 
        else 
        {  
           r.add("");  
        }  
        lastIndex = index;  
	    index = src.indexOf(sp, index + 1);  
	    if (index == -1) 
	    {  
	        r.add(src.substring(lastIndex + 1, src.length()));  
	    }  
    }  
    return r;  
 }

public static int validateLength(String subtitle) {
	int flag = 0;
	if (subtitle != null) {
		char[] a = subtitle.toCharArray();
		for (int i = 0; i < a.length; i++) {
			char c = a[i];
			if ((int) c >= 0 && (int) c <= 255)
				flag += 1;
			else
				flag += 2;
		}
	}
	return flag;
}

public static String substr(String subtitle, int length) {
	StringBuffer sb = new StringBuffer();
	int flag = 0;
	if (subtitle != null) {
		char[] a = subtitle.toCharArray();
		for (int i = 0; i < a.length; i++) {
			char c = a[i];
			if ((int) c >= 0 && (int) c <= 255)
				flag += 1;
			else
				flag += 2;
			if (flag > length) {
				break;
			} else {
				sb.append(c);
			}
		}
	}
	return sb.toString();
}  
//过滤特殊字符     
	public static String removeSpecialString(String str) {
		// 只允许字母和数字
		// String regEx = "[^a-zA-Z0-9]";
		// 清除掉所有特殊字符
		if(isEmpty(str))
			return "";
		String returns = "";
		try {
//			String regEx = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
			Pattern p = Pattern.compile(SPECIAL_STRING_REX);
			Matcher m = p.matcher(str);
			returns = m.replaceAll("").trim();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			return returns;
		}
	}
	//是否包含特殊字符
	public static boolean isContainSpecialString(String str) {
		// 只允许字母和数字
		// String regEx = "[^a-zA-Z0-9]";
		// 清除掉所有特殊字符
		if(isEmpty(str))
			return false;
		boolean returnf = false;
		try {
//			String regEx = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
			Pattern p = Pattern.compile(SPECIAL_STRING_REX);
			Matcher m = p.matcher(str);
			returnf = m.find();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			return returnf;
		}
	}
}